package qq.panel.gui.simple;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.TreeSet;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.border.LineBorder;

import qq.message.DataPackage;

	public class PanelSimpleGUI extends JFrame {
		
	private JTextArea chatTa;      //聊天显示框
	private DefaultListModel<String> defaultName;
	private JList friendsTa;   //朋友列表框
	private JTextArea sendTa;    //输入框
	private JButton sendBtn;   //发送
	private JButton deleteBtn; //删除
	private JButton shakeBtn;  //抖动按钮
	private JButton sendMass;   //群发 
	private JButton shutdown;
	private ImageIcon headPortrait; //头像
	private JLabel commentlabel;//包装头像
	private JLabel account;   //显示用户名
	private JLabel sign;   //显示签名
	private TreeSet<String> names;
	private Timer t;
	private String myname;
	private BackPackageMain bpm;
	
	public BackPackageMain getBpm() {
		return bpm;
	}
	public void setBpm(BackPackageMain bpm) {
		this.bpm = bpm;
	}
	public JTextArea getChatTa() {
		return chatTa;
	}
	public void setChatTa(JTextArea chatTa) {
		this.chatTa = chatTa;
	}
	public JList getFriendsTa() {
		return friendsTa;
	}
	public void setFriendsTa(JList friendsTa) {
		this.friendsTa = friendsTa;
	}
	public PanelSimpleGUI(String username) {
		bpm=new BackPackageMain();
		names=new TreeSet<String>();
		myname=username;
		this.setSize(600, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		JFrame.setDefaultLookAndFeelDecorated(isDefaultLookAndFeelDecorated());
		JFrame.setDefaultLookAndFeelDecorated(true);
		init(username);
		addEvent();
		PanelSimpleGUI.this.setLocation(600,300);
		this.setVisible(true);
			
	}
	void init(String username){
		this.setLayout(new BorderLayout());
		JPanel panLeft=new JPanel(new BorderLayout());   //左侧panel
		JPanel panRight=new JPanel(new BorderLayout());  //右侧panel
		JPanel panLeftBottom=new JPanel(new BorderLayout());  //左侧底部构建
		JPanel panBtnGroup=new JPanel(new BorderLayout());   //左侧底部西边按钮组
		JPanel panlRightTop=new JPanel(new BorderLayout());   //右侧顶端
		JPanel panlRightTopRight=new JPanel(new BorderLayout());   //右侧顶端右侧账号签名
		JPanel panlRightBottom=new JPanel(new BorderLayout());   //右侧顶端右侧账号签名
		chatTa=new JTextArea(10,20);
		chatTa.setLineWrap(true);
		friendsTa=new JList();
		sendTa=new JTextArea(4,7);
		deleteBtn=new JButton("Delete");
		sendBtn=new JButton("Send");
		shakeBtn=new JButton("Shake");
		sendMass=new JButton("Send All");
		shutdown=new JButton("Close");
		headPortrait=new ImageIcon("gxr1000.jpg");
	    commentlabel = new JLabel("", headPortrait,JLabel.CENTER);
	    commentlabel.setBounds(450, 20, 60, 60);
	    account=new JLabel(username.trim());
	    sign=new JLabel("someone");
			
        account.setFont(new Font("隶书",Font.PLAIN, 16));
        account.setForeground(Color.darkGray);
		sendTa.setFont(new Font("隶书",Font.PLAIN, 16));
		sendTa.setForeground(Color.darkGray);
		sendTa.setFont(new Font("隶书",Font.PLAIN, 16));
		sendTa.setForeground(Color.darkGray);
		chatTa.setFont(new Font("隶书",Font.PLAIN, 16));
		chatTa.setForeground(Color.darkGray);
		sendBtn.setFont(new Font("隶书",Font.PLAIN, 16));
		sendBtn.setForeground(Color.darkGray);
		shakeBtn.setFont(new Font("隶书",Font.PLAIN, 16));
		shakeBtn.setForeground(Color.darkGray);
		deleteBtn.setFont(new Font("隶书",Font.PLAIN, 16));
		deleteBtn.setForeground(Color.darkGray);
		sendMass.setFont(new Font("隶书",Font.PLAIN, 16));
		sendMass.setForeground(Color.darkGray);
		shutdown.setFont(new Font("隶书",Font.PLAIN, 16));
		shutdown.setForeground(Color.darkGray);
			
		account.setFont(new Font("隶书",Font.PLAIN, 16));
		account.setForeground(Color.darkGray);
		sign.setFont(new Font("隶书",Font.PLAIN, 16));
		sign.setForeground(Color.darkGray);
			
			
		friendsTa.setFont(new Font("隶书",Font.PLAIN, 26));
		friendsTa.setForeground(Color.darkGray);
			
		//设置好友框List
//		names.add(username);
		names.add("  Friend Online  ");
//		names.add("allice");
//		names.add("jhone");
		defaultName = new DefaultListModel<String>();
		Iterator<String> iter = names.iterator();
		while(iter.hasNext()) {
			defaultName.addElement(iter.next().trim());
		}
		
	    friendsTa=new JList(defaultName);
	    friendsTa.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	    friendsTa.setSelectedIndex(0);
	    friendsTa.setVisibleRowCount(3);
		//设置背景
		panlRightTopRight.setBackground(Color.lightGray);
		//设置边框
		panRight.setBorder(new LineBorder(Color.gray));
		panLeft.setBorder(new LineBorder(Color.gray));
		panLeftBottom.setBorder(new LineBorder(Color.gray));
		panBtnGroup.setBorder(new LineBorder(Color.gray));
		panlRightBottom.setBorder(new LineBorder(Color.gray));
		sendTa.setBorder(new LineBorder(Color.gray));
		commentlabel.setBorder(new LineBorder(Color.gray));
		//设置构建顺序
		panlRightTopRight.add(account,BorderLayout.CENTER);
		panlRightTopRight.add(sign,BorderLayout.SOUTH);
		panlRightTop.add(commentlabel,BorderLayout.WEST);
		panlRightTop.add(panlRightTopRight,BorderLayout.EAST);
		panRight.add(panlRightTop,BorderLayout.NORTH);
		panRight.add(new JScrollPane(friendsTa),BorderLayout.CENTER);
		panlRightBottom.add(sendMass,BorderLayout.NORTH);
		panlRightBottom.add(shutdown,BorderLayout.SOUTH);
		panLeft.add(new JScrollPane(chatTa), BorderLayout.CENTER);
		panLeft.add(panLeftBottom,BorderLayout.SOUTH);

		this.add(panLeft,BorderLayout.CENTER);
		this.add(panRight,BorderLayout.EAST);
		panRight.add(panlRightBottom,BorderLayout.SOUTH);
		panBtnGroup.add(shakeBtn,BorderLayout.CENTER);
		panBtnGroup.add(deleteBtn,BorderLayout.NORTH);
		panBtnGroup.add(sendBtn,BorderLayout.SOUTH);
		panLeftBottom.add(new JScrollPane(sendTa),BorderLayout.CENTER);
		panLeftBottom.add(panBtnGroup,BorderLayout.EAST);
	}
	
	    
	
	/**
	 * 顺序排列好友
	 * @param names
	 */
	public void resetList(TreeSet<String> nameSet)
	{
		  System.out.println(nameSet);
		  Iterator<String> i = nameSet.iterator();
		  while(i.hasNext()) {
			  String[] s=i.next().split("=");
			  System.out.println(s[0]);
			  if(!names.contains(s[0].trim()))
				   names.add(s[0].trim());
		  }
		  Iterator<String> in = names.iterator();
		  defaultName.clear();
		  while(in.hasNext()) {
			  String s=in.next();
			  defaultName.addElement(s.trim());
		  }
		  System.out.println(names);
		  friendsTa.updateUI();
	}
	
	public void close()
	{
		this.dispose();
	}
	
	//抖动动作
	public void shake() {
		// TODO Auto-generated method stub
		//计时器javax.swing.Timer
		
		  int delay = 30; //milliseconds
		  ActionListener taskPerformer = new ActionListener()
		        {
			  		int x=PanelSimpleGUI.this.getX();
			  		int y=PanelSimpleGUI.this.getY();
		            int j = 0;
		            public void actionPerformed(ActionEvent e)
		            {
		               
		                if(j<=12)
		                {
		                 if(j==0)
		                    {
		                PanelSimpleGUI.this.setLocation(x,y);
		                    }
		                    else if(j==1)
		                    {
		                 PanelSimpleGUI.this.setLocation(x+1,y-1);
		                    }
		                    else if(j==2)
		                    {
		                 PanelSimpleGUI.this.setLocation(x+2,y-2);
		                    }
		                    else if(j==3)
		                    {
		                 PanelSimpleGUI.this.setLocation(x+3,y-3);
		                    }
		                    else if(j==4)
		                    {
		                 PanelSimpleGUI.this.setLocation(x+2,y-4);
		                    }
		                    else if(j==5)
		                    {
		                 PanelSimpleGUI.this.setLocation(x+1,y-5);
		                    }
		                    else if(j==6)
		                    {
		                 PanelSimpleGUI.this.setLocation(x,y-6);
		                    }
		                    else if(j==7)
		                    {
		                 PanelSimpleGUI.this.setLocation(x-1,y-5);
		                    }
		                    else if(j==8)
		                    {
		                 PanelSimpleGUI.this.setLocation(x-2,y-4);
		                    }
		                    else if(j==9)
		                    {
		                 PanelSimpleGUI.this.setLocation(x-3,y-3);
		                    }
		                    else if(j==10)
		                    {
		                 PanelSimpleGUI.this.setLocation(x-2,y-2);
		                    }
		                    else if(j==11)
		                    {
		                 PanelSimpleGUI.this.setLocation(x-1,y-1);
		                    }
		                }
		                j++;
		                if(j>12)
		                {
		                 j=0;
		                 try {
							t.stop();
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							System.out.println("shake error about Timer initial");
						}
		                }
		            }
		        };
		        t = new Timer(delay, taskPerformer);
		        t.start();
	}

public void addEvent(){
		
		sendBtn.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				StringBuilder str=new StringBuilder();
				str.append(myname+"  ");
				str.append(new Date(System.currentTimeMillis()).toString());
				str.append("\n  ");
				str.append(sendTa.getText());
				str.append("\n");
				String content=sendTa.getText();
				sendTa.setText("");
				System.out.println("send action");
//				chatTa.append(content+"\n");
				bpm.setMessage(content);
				bpm.setType(1);
				bpm.setStatue(1);
				System.out.println(friendsTa.getSelectedIndex());
				if(friendsTa.getSelectedIndex()>0)  //规避头行显示朋友列表被点击
				{
					bpm.setToName(friendsTa.getSelectedValue().toString().trim());
					System.out.println("click a alive user");
				}
				else
					bpm.setToName("\\");
				
			}
		});
		
		shakeBtn.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("shake action");
				bpm.setMessage("");
				bpm.setType(3);
				bpm.setStatue(1);
				if(friendsTa.getSelectedIndex()>0)
				{
					bpm.setToName(friendsTa.getSelectedValue().toString().trim());
				}
				else
					bpm.setToName("\\");
			}
		});
		
		deleteBtn.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("cancel action");
				sendTa.setText("");
				
			}
		});
		
		sendMass.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("sendMass action");
				String content=sendTa.getText();
				sendTa.setText("");
				bpm.setMessage(content);
				bpm.setType(2);
				bpm.setStatue(1);
				bpm.setToName("\\");
			}
		});
		shutdown.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("close action");
				sendTa.setText("");
				bpm.setMessage("");
				bpm.setType(4);
				bpm.setStatue(1);
				bpm.setToName("server");
			}
		});
		
	}
}